package com.gxa.finance.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gxa.finance.config.RabbitmqConfig;
import com.gxa.order.feign.ServiceOrderApi;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class RabbitmqListener {

    @Autowired
    ServiceOrderApi serviceOrderApi;

    //监听死信队列
    @RabbitListener(queues = {RabbitmqConfig.DEAD_LETTER_QUEUE})
    public void listen(Map<String,Object> map) throws JsonProcessingException {

        int orderNumber = Integer.parseInt(map.get("orderType").toString());

        //判断订单类型
        if(orderNumber == 1){ //住宿单
            //根据订单编号查询对应的订单id
            int id = (Integer) serviceOrderApi.findid(map.get("number").toString()).getData();
            //根据订单id查询订单状态
            int status = (Integer)serviceOrderApi.findOrderStatu(id).getData();
            //判断是否修改状态
            if(status == 1){
                serviceOrderApi.updatestatu(9,id);
            }
        }
        else{//服务单
            //根据订单编号查询对应的订单id
            int id = (Integer)serviceOrderApi.findOrderIdByNumber(map.get("number").toString()).getData();
            //根据订单id查询订单状态
            Object data = serviceOrderApi.findCleanOrder5(id).getData();
            //利用springboot自带的jackson将object转成map
            ObjectMapper mapper = new ObjectMapper();
            Map map01 = mapper.readValue(mapper.writeValueAsString(data), Map.class);
            int status = Integer.parseInt(map01.get("status").toString());
            //判断是否修改状态
            if(status == 1){
                serviceOrderApi.delCleanOrder(id);
            }
        }
    }

}